# coding: utf-8
# Author: legend
# @Time： 2020/8/27 下午10:05
import logging

from utils.messages.handler import BaseHandler
from websocket.messages import LatestDevicePartitionStatusResponse, MultiDevicePartitionStatusResponse

logger = logging.getLogger('app.running.log')


class LatestDevicePartitionStatusRequestHandler(BaseHandler):

    async def do(self, *args, **kwargs):
        device_id = kwargs.get('device_id')
        result_value = self.get_monitor_data(0, device_id, 'partition.status', 1, True)
        for result_item in result_value:
            result_item.update({'file_number': int(23471.1 * float(result_item.get('used_percent')))})
        return {
            'data': result_value,
            'result_code': LatestDevicePartitionStatusResponse.ResultCode.NORMAL
        }


class MultiDevicePartitionStatusRequestHandler(BaseHandler):

    async def do(self, *args, **kwargs):
        device_id = kwargs.get('device_id')
        size = kwargs.get('size')
        result_value = self.get_monitor_data(0, device_id, 'partition.status', size, True)
        for result_item in result_value:
            for temp_item in result_item:
                temp_item.update({'file_number': int(23471.1 * float(temp_item.get('used_percent')))})
        return {
            'result_code': MultiDevicePartitionStatusResponse.ResultCode.NORMAL,
            'data': list(reversed(result_value))
        }
